package com.polaris.live.popup

import android.content.Context
import android.view.View
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.AttachPopupView
import com.polaris.live.databinding.PopupMusicVolumeBinding
import com.polaris.live.utils.zego.media.MediaProxy

/**
 * MusicVolumePopup 音乐音量调节
 *
 * @author Created by 长空 on 2023/10/25/004 17:21
 */
class MusicVolumePopup(
    context: Context,
) : AttachPopupView(context) {

    private lateinit var mBinding: PopupMusicVolumeBinding

    private var volume = 60

    override fun getImplLayoutId(): Int {

        return com.polaris.live.R.layout.popup_music_volume
    }

    override fun onCreate() {
        super.onCreate()

        mBinding = PopupMusicVolumeBinding.bind(popupImplView)
        volume = MediaProxy.getPlayVolume()
        mBinding.progress.setMax(100)
        mBinding.progress.setProgress(volume)
        mBinding.progressTv.text = "$volume%"
        mBinding.progress.setSeekToProgress {
            volume = it.toInt()
            setVolume(volume)
        }
        mBinding.add.setOnClickListener {
            if (volume <= 99) {
                volume++
                setVolume(volume)
            }
        }
        mBinding.remove.setOnClickListener {
            if (volume >= 1) {
                volume--
                setVolume(volume)
            }
        }
    }

    private fun setVolume(volume: Int) {
        MediaProxy.setPlayVolume(volume)
        MediaProxy.setPublishVolume(volume)
        mBinding.progressTv.text = "$volume%"
    }

    companion object {

        fun show(context: Context, view: View) {
            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .enableDrag(false)
                .atView(view)
//                .offsetY((-30).autoDp)
                .hasShadowBg(false)
                .asCustom(MusicVolumePopup(context))
                .show()

        }
    }
}